<?php


namespace App\Services;


use App\Models\AddressModel;
use App\Models\HouseAudit;
use App\Models\HouseLike;
use App\Models\HouseList;
use App\Models\HouseType;
use App\Models\RentAndPurchase;
use App\Models\WxUser;
use App\Tools\JsonTools;
use App\Tools\RedisTools;

class HouseServices
{
    public $redis;

    /**
     * HouseServices constructor.
     * Redis
     */
    public function __construct()
    {
        $this->redis = (RedisTools::getInstance())->connectRedis();
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/12
     * @Time: 9:56
     * @return array
     * 获取房屋类型Service
     */
    public static function houseType()
    {
        $typeModel = HouseType::get();
        if (empty($typeModel)) {
            return JsonTools::JsonError($typeModel);
        }
        return JsonTools::JsonSuccess($typeModel);
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/12
     * @Time: 9:57
     * @param $param
     * @param $uid
     * @return array
     * 添加求租求购
     */
    public static function houseAddService($param, $uid)
    {

        $param['house_city'] = $param['house_region'][1];
        $param['house_region_list'] = $param['house_region'][2];
        $param['house_region'] = implode("", $param['house_region']);
        $param['uid'] = $uid;
        $param['house_price_type'] = 0;//默认的支付状态
        $time = date("Y-m-d 00:00:00", time() + 86400 * 30);//默认三十天
        $param['house_end_time'] = $time;
        $createModel = RentAndPurchase::create($param);//添加
        if (empty($createModel)) {
            return JsonTools::JsonError();
        }
        return JsonTools::JsonSuccess($createModel);
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/12
     * @Time: 9:57
     * @param $house_rent_and_purchase
     * @return array
     * 展示求租求购详情
     */
    public function listModel($house_rent_and_purchase, $address, $type, $region)
    {
        $where = [];
        if (!empty($address)) {
            $where[] = [
                "house_city", "$address"
            ];
        }
        if (!empty($region)) {
            $where[] = [
                "house_region_list", "$region"
            ];
        }
        if (!empty($type)) {
            $where[] = [
                "house_type", "$type"
            ];
        }
        //查询条件满足 并且满足支付成功 租房和购房的类型
        $model = RentAndPurchase::where($where)->where('house_price_type', 1)->where("house_rent_and_purchase",$house_rent_and_purchase)
            ->get(['id'])->toArray();
        if (empty($model)) {
            return JsonTools::JsonError();
        }
        foreach ($model as $k => $value) {
            $list[] = $this->redis->hGet("RentAndPurchase", $value['id']);
        }
        $listRedis = [];
        foreach ($list as $value) {
            $value = json_decode($value, true);//根据id读取哈希表中的数据
            if ($value['house_rent_and_purchase'] == $house_rent_and_purchase) {
                $listRedis[] = $value;
            }
        }

        if (empty($listRedis)) {
            return JsonTools::JsonError();
        }
        return JsonTools::JsonSuccess($listRedis);
    }

    /**
     * @Notes:获取房屋审核信息
     * @Author:Planb,
     * @Date: 2022/11/12,
     * @Time: 9:55,
     */
    public static function getHouseAuditInfo($houseId)
    {
        $houseAuditInfo = HouseAudit::where('id', $houseId)->first();
        if ($houseAuditInfo) {
            return $houseAuditInfo;
        } else {
            return false;
        }
    }

    /**
     * @Notes:获取二手房源列表
     * 没有经纪人的不展示
     * @Author:Planb,
     * @Date: 2022/11/14,
     * @Time: 14:25,
     */
    public function getHouseListServer($limit, $offset, $city)
    {
        $currentId = $offset + 1;
        $houseListCount = HouseList::where('house_brokers', '!=', '')->where('house_city', $city)->count();
        $data = $this->redis->zRangeByScore('houseList' . $city, $currentId, $currentId + $limit - 1);
        if ($data) {
            foreach ($data as $v) {
                $result[] = json_decode($v, true);
            }
            return [
                'houseList' => $result,
                'houseCount' => $houseListCount
            ];
        }
        $houseList = HouseList::where('house_brokers', '!=', '')->where('house_city', $city)->limit($limit)->offset($offset)->get()->toArray();
        if ($houseList) {
            foreach ($houseList as $value) {
                $this->redis->zAdd('houseList' . $city, $value['id'], json_encode($value, 256));
            }
            return [
                'houseList' => $houseList,
                'houseCount' => $houseListCount
            ];
        } else {
            return false;
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 14:49
     * 获取当前的市
     */
    public function addressRegion($param)
    {
        if (!is_numeric($param)) {
            return JsonTools::JsonError();
        }
        $model = AddressModel::where('pid', $param)->get();
        if ($model) {
            return JsonTools::JsonSuccess($model);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 15:58
     * @return array
     * 获取所有市
     */
    public function addressProvince()
    {
        $model = AddressModel::where('name', 'like', '%市')->get();
        if ($model) {
            return JsonTools::JsonSuccess($model);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 19:14
     * @param $uid
     * @return array
     * 获取用户电话
     */
    public function userPhone($uid)
    {
        $model = WxUser::where('id', $uid)->first();
        if ($model) {
            return JsonTools::JsonSuccess($model);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/16
     * @Time: 19:14
     * @param $param
     * @return array
     * 删除过期数据
     */
    public function redisDel($param)
    {
        foreach ($param as $k => $value) {
            $this->redis->hDel("RentAndPurchase", $value['id']);
        }
        return JsonTools::JsonSuccess();
    }

    /**
     * @Notes:
     * @Author: 遇见
     * @Date: 2022/11/24
     * @Time: 13:43
     * @param $param
     * @return array
     * 获取详细信息
     */
    public function houseList($param){
        $model = $this->redis->hGet('RentAndPurchase',$param);
        $model = json_decode($model,true);
        if(!$model){
            return JsonTools::JsonError();
        }
        return JsonTools::JsonSuccess($model);
    }


    /**
     * @Notes:收藏
     * @Author:Planb,
     * @Date: 2022/12/2,
     * @Time: 20:00,
     */
    public static function likeHouseInfo($uid,$houseId){
            $houseInfo = HouseLike::where('uid',$uid)->where('house_id',$houseId)->first();
            if ($houseInfo){
                if ($houseInfo['status']==1){
                    //存在修改
                    $houseResult = HouseLike::where('uid',$uid)->where('house_id',$houseId)->update([
                        'status'=> 0
                    ]);
                    return StatusNumber::UPDATESUCCESS;
                }else{
                    $houseResult = HouseLike::where('uid',$uid)->where('house_id',$houseId)->update([
                        'status'=> 1
                    ]);
                    return StatusNumber::LIKESUCCESS;
                }

            }else{
                //不存在添加
                $houseResult = HouseLike::insert([
                   'uid'=>$uid,
                   'house_id'=>$houseId,
                   'status'=>1,
                    'created_at'=>date('Y-m-d H:i:s')
                ]);
                if ($houseResult){
                    return StatusNumber::LIKESUCCESS;
                }else{
                    return StatusNumber::LIKEFAIL;
                }
            }
    }
}
